import { request } from '../../services/index.js'

var phone = ''
var code = ''
var referee = ''
Component({
    externalClasses: ['otherclass'],
    behaviors: [],

    // name object
    // type string
    // value string
    // observer function
    properties: {
        isappear: {
            type: Boolean
        }
    },
    data: {
        words: '获取验证码',
        time: 60
    }, // 私有数据，可用于模版渲染

    // 生命周期函数，可以为函数，或一个在methods段中定义的方法名
    attached: function () { },
    moved: function () { },
    detached: function () { },

    methods: {
        // 处理关闭
        onhandleClose: function () {
            this.setData({
                isappear: false
            })
        },

        // 处理获取验证码
        onhandleGetCode: function () {
            let val = this.data.time
            if (val == 59) {
                const userinfo = wx.getStorageSync('_USERINFO_')
                const openid = userinfo.openid
                request('sms/sendCode', 'POST', { openid, mobile: phone }, (res) => {
                    console.log(res)
                })
            }
            if (val == 0) {
                this.setData({
                    time: 60,
                    words: '获取验证码'
                })
                return false
            } else {
                val--
                this.setData({
                    time: val,
                    words: `${val}秒可重发`
                })
            }
            setTimeout(() => {
                this.onhandleGetCode()
            }, 1000)
        },

        // 处理输入框的值
        onhandleInput: function (e) {
            const currt = e.target.id
            const val = e.detail.value
            switch (currt) {
                case 'phone':
                    phone = val
                    break
                case 'code':
                    code = val
                    break
                case 'referee':
                    referee = val
                    break
            }
        },

        // 处理注册
        onhandleRegister: function () {
            const userinfo = wx.getStorageSync('_USERINFO_')
            const openid = userinfo.openid
            request('sms/bindPhone', 'POST', { openid, mobile: phone, captcha: code, invitecode: referee }, (res) => {
                console.log(res)
            })
        }
    }
})